package xyc.szk.web.servlet.request;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-03-26
 * @Time: 14:26
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String username = req.getParameter("username");//获取浏览器请求（发送给服务器）的参数（账户）
//        String userpwd = req.getParameter("userpwd");//获取浏览器请求（发送给服务器）的参数（密码）
//        System.out.println("用户账户："+username+"账户密码："+userpwd);

        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = req.getParameter(name);
            System.out.println("用户名称：" + name + "用户密码：" + value);
        }

        //get请求参数时要经过tomcat服务器进行解码 服务器解码也要用同样的编码进行解码
        //tomcat8.0及以上get请求已经有它处理好了，服务器不要坐任何操作
//        Map<String, String[]> map = req.getParameterMap();
//        map.forEach((k,v)->System.out.println(k+":"+ Arrays.toString(v)));

        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        if (username.equals("admin")&&userpwd.equals("123")) {
            //请求转发到success.html页面
            RequestDispatcher dispatcher = req.getRequestDispatcher("/success.html");
            req.setAttribute("login","admin");
            dispatcher.forward(req,resp);
        } else {
            //请求转发到fail.html页面

            // RequestDispatcher dispatcher = req.getRequestDispatcher("/fail.html");
            // dispatcher.forward(req,resp);

            //动态获取web项目的虚拟目录：/04
            String path = req.getContextPath();// ->/04

//            resp.setStatus(302);
//            resp.setHeader("location",path+"/fail.html");

            resp.sendRedirect(path+"/fail.html");

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String username = req.getParameter("username");//获取浏览器请求（发送给服务器）的参数（账户）
//        String userpwd = req.getParameter("userpwd");//获取浏览器请求（发送给服务器）的参数（密码）
//        System.out.println("用户账户："+username+"账户密码："+userpwd);

//        Enumeration<String> parameterNames = req.getParameterNames();
//        while (parameterNames.hasMoreElements()) {
//            String name = parameterNames.nextElement();
//            String value = req.getParameter(name);
//            System.out.println("用户名称：" + name + "用户密码：" + value);
//        }

        //post请求会在服务器接受参数是出现乱码，因为浏览器请求的参数编码方式和页面一致，服务器解码时也要用同一种编码进行
        req.setCharacterEncoding("utf-8"); //用utf-8来解码
        Map<String, String[]> map = req.getParameterMap();
        map.forEach((k,v)->System.out.println(k+":"+ Arrays.toString(v)));
        //String[] parameterValues = req.getParameterValues("aulogin");
    }
}
